﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace DataCollection
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public Socket newclient;
        public bool Connected;
        public delegate void MyInvoke(string str);
        private static DispatcherTimer timersend = new DispatcherTimer();
        private static DispatcherTimer timersend1 = new DispatcherTimer();
        public string stringdata;
        public Thread mThread;
        public ThreadStart myThreadStart;
        delegate void SetTextCallback(string text);
        private SetTextCallback d;
        private byte[] data;
        public MainWindow()
        {
            InitializeComponent();

        }

        //检测IP地址是否正确
        private Boolean initIP()
        {
            Regex validipregex = new Regex(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
            if (serverIP.Text.Trim() != null && serverIP.Text.Trim().Length != 0 && serverIP.Text.Trim() != "" && validipregex.IsMatch(serverIP.Text.Trim()))
            {
                return true;
            }
            else
            {
                MessageBox.Show("服务器地址输入有误，请重新输入");
                return false;
            }
        }
        //创建连接
        public void Connect()
        {
            byte[] data = new byte[1024];

            //获取输入框服务地址和端口号
            String IP = serverIP.Text.Trim();
            int Port = Convert.ToInt32(port.Text.Trim());

            //创建连接套词
            IPEndPoint ie = new IPEndPoint(IPAddress.Parse("192.168.51.199"), 502);


            newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //尝试连接，否则抛出异常
            try
            {


                //进行服务器连接
                newclient.Connect(ie);

                connect.IsEnabled = false;

                Connected = true;


            }
            catch (SocketException e)
            {
                MessageBox.Show("连接服务器失败" + e.Message);
                return;
            }
            //timersend.Tick += new EventHandler(SendFanMotorStartMsg);
            //timersend.Tick += new EventHandler(SendExhaustMsg);
            //timersend.Tick += new EventHandler(SendOrdinaryDowntimeMsg);
            //timersend.Tick += new EventHandler(SendAutomaticRunningMsg);
            //timersend.Tick += new EventHandler(SendNormalWarningMsg);
            //timersend.Tick += new EventHandler(SendAllowsStartingTheMainMotorMsg);
            //timersend.Tick += new EventHandler(SendCompressorOutletMsg);
            //timersend.Tick += new EventHandler(SendHostOutletMsg);
            //timersend.Tick += new EventHandler(SendAmbientAirTemperatureMsg);
            //timersend.Tick += new EventHandler(SendEmergencyShutdownMsg);
            //timersend.Tick += new EventHandler(SendFanMotorOverloadMsg);
    //        timersend.Tick += new EventHandler(SendRemoteStartMsg);
            //timersend.Tick += new EventHandler(SendPressureSettingMsg);
            //timersend.Tick += new EventHandler(SendVSDSetpointFirstMsg);
            //timersend.Tick += new EventHandler(SendFirstIndirectStopMsg);
            //timersend.Tick += new EventHandler(SendFirstDirectStopMsg);
            //timersend.Tick += new EventHandler(SendVSDSetpointSecondMsg);
            //timersend.Tick += new EventHandler(SendSecondIndirectStopMsg);
            //timersend.Tick += new EventHandler(SendSecondDirectStopMsg);
            //timersend.Tick += new EventHandler(SendSetpointSelectionMsg);
            //timersend.Tick += new EventHandler(SendPressureBandSelectionMsg);
            timersend.Tick += new EventHandler(SendOperationTimeMsg);
            timersend.Tick += new EventHandler(SendMotorStartTimesMsg);
            timersend.Tick += new EventHandler(SendLoadRelayMsg);
            timersend.Tick += new EventHandler(SendFirstDriveSpeedMsg);
            timersend.Tick += new EventHandler(SendSecondDriveSpeedMsg);
            timersend.Tick += new EventHandler(SendThirdDriveSpeedMsg);
            timersend.Tick += new EventHandler(SendFourthDriveSpeedMsg);
            timersend.Tick += new EventHandler(SendFifthDriveSpeedMsg);
            timersend.Tick += new EventHandler(SendFanStartMsg);
            timersend.Tick += new EventHandler(SendCumulativeFlowMsg);
            timersend.Tick += new EventHandler(SendControllerOperatingTimeMsg);

            timersend.Interval = new TimeSpan(0, 0, 0, 1);
            timersend.Start();

            //     timersend.IsEnabled = true;

        }


        //采集风扇马达启动开关
        private void SendFanMotorStartMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x09, 0xf6, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveFanMotorStarMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveFanMotorStarMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x01)
                {
                     d = new SetTextCallback(SetFanMotorStarText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
  //  delegate void SetFanMotorStartTextCallback(string text);
        private void SetFanMotorStarText(string text)
        {
            fanMotorStart.Text = text;
        }


        //采集排气开关
        private void SendExhaustMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x02, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x09, 0xf9, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveExhaustMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start();
            mThread.DisableComObjectEagerCleanup();
        }
      //  delegate void SetExhaustTextCallback(string text);
        //接收参数
        private void ReceiveExhaustMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x02)
                {
                     d = new SetTextCallback(SetExhaustText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
        private void SetExhaustText(string text)
        {
            exhaust.Text = text;
        }


        // 采集普通停机开关
        private void SendOrdinaryDowntimeMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x09, 0xfa, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveOrdinaryDowntimeMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }
    //    delegate void SetOrdinaryDowntimeTextCallback(string text);
        //接收参数
        private void ReceiveOrdinaryDowntimeMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x03)
                {
                     d = new SetTextCallback(SetOrdinaryDowntimeText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
        private void SetOrdinaryDowntimeText(string text)
        {
            ordinaryDowntime.Text = text;
        }


        // 采集自动运行开关
        private void SendAutomaticRunningMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x04, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x09, 0xfc, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveAutomaticRunningMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }
     //   delegate void SetAutomaticRunningTextCallback(string text);
        //接收参数
        private void ReceiveAutomaticRunningMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x04)
                {
                     d = new SetTextCallback(SetAutomaticRunningText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
        private void SetAutomaticRunningText(string text)
        {
            automaticRunning.Text = text;
        }


        // 采集普通警告开关
        private void SendNormalWarningMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x09, 0xfd, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveNormalWarningMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }
      //  delegate void SetNormalWarningTextCallback(string text);
        //接收参数
        private void ReceiveNormalWarningMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x05)
                {
                     d = new SetTextCallback(SetNormalWarningText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
        private void SetNormalWarningText(string text)
        {
            normalWarning.Text = text;
        }

        // 采集允许启动运行主马达开关
        private void SendAllowsStartingTheMainMotorMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x09, 0xfe, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveAllowsStartingTheMainMotorMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }
     //   delegate void SetAllowsStartingTheMainMotorTextCallback(string text);
        //接收参数
        private void ReceiveAllowsStartingTheMainMotorMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x06)
                {
                     d = new SetTextCallback(SetAllowsStartingTheMainMotorText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
        private void SetAllowsStartingTheMainMotorText(string text)
        {
            allowsStartingTheMainMotor.Text = text;
        }

        // 采集压缩机出口信息
        private void SendCompressorOutletMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x07, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x00, 0x00, 0x02 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveCompressorOutletMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }
     //   delegate void SetCompressorOutletTextCallback(string text);
        //接收参数
        private void ReceiveCompressorOutletMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x07)
                {
                     d = new SetTextCallback(SetCompressorOutletText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
        private void SetCompressorOutletText(string text)
        {
            compressorOutlet.Text = text;
        }

        // 采集主机出口信息
        private void SendHostOutletMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x08, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x04, 0x00, 0x02 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveHostOutletMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }
   //     delegate void SetHostOutletTextCallback(string text);
        //接收参数
        private void ReceiveHostOutletMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x08)
                {
                     d = new SetTextCallback(SetHostOutletText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
        private void SetHostOutletText(string text)
        {
            hostOutlet.Text = text;
        }



        // 采集环境空气温度信息
        private void SendAmbientAirTemperatureMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x09, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x08, 0x00, 0x02 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveAmbientAirTemperatureMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }
      //  delegate void SetAmbientAirTemperatureTextCallback(string text);
        //接收参数
        private void ReceiveAmbientAirTemperatureMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x09)
                {
                     d = new SetTextCallback(SetAmbientAirTemperatureText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
        private void SetAmbientAirTemperatureText(string text)
        {
            ambientAirTemperature.Text = text;
        }




        //采集紧急停机开关
        private void SendEmergencyShutdownMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x0a, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x01, 0xf4, 0x00, 0x02 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveEmergencyShutdownMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveEmergencyShutdownMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x0a)
                {
                     d = new SetTextCallback(SetEmergencyShutdownText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
    //    delegate void SetEmergencyShutdownTextCallback(string text);
        private void SetEmergencyShutdownText(string text)
        {
            emergencyShutdown.Text = text;
        }


        //采集风扇电机过载开关
        private void SendFanMotorOverloadMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x0b, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x01, 0xf6, 0x00, 0x02 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveFanMotorOverloadMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveFanMotorOverloadMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x0b)
                {
                     d = new SetTextCallback(SetFanMotorOverloadText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
      //  delegate void SetFanMotorOverloadTextCallback(string text);
        private void SetFanMotorOverloadText(string text)
        {
            fanMotorOverload.Text = text;
        }



     //   //采集远程启动/停机开关(未用到)
     //   private void SendRemoteStartMsg(object sender, EventArgs e)
     //   {
     //       byte[] data = new byte[] { 0x00, 0x0c, 0x00, 0x00, 0x00, 0x06, 0x01, 0x01, 0x01, 0xf6, 0x00, 0x01 };
     //       newclient.Send(data);
     //       myThreadStart = new ThreadStart(ReceiveRemoteStartMsg);
     //       mThread = new Thread(myThreadStart);
     //       mThread.Start(); mThread.DisableComObjectEagerCleanup();
     //   }

     //   //接收参数
     //   private void ReceiveRemoteStartMsg()
     //   {
     //     //  while (true)
     //       {
     //           byte[] data = new byte[1024];//定义数据接收数组  
     //           newclient.Receive(data);//接收数据到data数组  
     //           int length = data[5];//读取数据长度  
     //           Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
     //           for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
     //               datashow[i] = data[i];
     //           stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
     //           if (data[1] == 0x0c)
     //           {
     //               SetTextCallback d = new SetTextCallback(SetRemoteStartText);   // 托管调用  
     //               this.Dispatcher.Invoke(d, new object[] { stringdata });
     //           }

     //       }
     //   }
     ////   delegate void SetRemoteStartTextCallback(string text);
     //   private void SetRemoteStartText(string text)
     //   {
     //       fanMotorOverload.Text = text;
     //   }

        //采集压力设定选择开关
        private void SendPressureSettingMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x0d, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x02, 0x06, 0x00, 0x02 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceivePressureSettingMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceivePressureSettingMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x0d)
                {
                     d = new SetTextCallback(SetPressureSettingText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
      //  delegate void SetPressureSettingTextCallback(string text);
        private void SetPressureSettingText(string text)
        {
            pressureSetting.Text = text;
        }

        //Regulation Parameters调节参数设定，包括设定点1、2，已经它们的停机点、系统选择的设定点
        //采集设定点1数据
        private void SendVSDSetpointFirstMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x0e, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x53, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveVSDSetpointFirstMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveVSDSetpointFirstMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x0e)
                {
                     d = new SetTextCallback(SetVSDSetpointFirstText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
    //    delegate void SetVSDSetpointFirstTextCallback(string text);
        private void SetVSDSetpointFirstText(string text)
        {
            VSDSetpointFirst.Text = text;
        }


        //采集设定点1间接停机点数据
        private void SendFirstIndirectStopMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x0f, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x71, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveFirstIndirectStopMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveFirstIndirectStopMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x0f)
                {
                     d = new SetTextCallback(SetFirstIndirectStopText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
    //    delegate void SetFirstIndirectStopTextCallback(string text);
        private void SetFirstIndirectStopText(string text)
        {
            FirstIndirectStop.Text = text;
        }


        //采集设定点1直接停机点数据
        private void SendFirstDirectStopMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x10, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x72, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveFirstDirectStopMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveFirstDirectStopMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x10)
                {
                    d = new SetTextCallback(SetFirstDirectStopText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });

                }
            }
        }
    //    delegate void SetFirstDirectStopTextCallback(string text);
        private void SetFirstDirectStopText(string text)
        {
            FirstDirectStop.Text = text;
        }


        //采集设定点2数据
        private void SendVSDSetpointSecondMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x11, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x54, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveVSDSetpointSecondMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveVSDSetpointSecondMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x11)
                {
                     d = new SetTextCallback(SetVSDSetpointSecondText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });

                }
            }
        }
    //    delegate void SetVSDSetpointSecondTextCallback(string text);
        private void SetVSDSetpointSecondText(string text)
        {
            VSDSetpointSecond.Text = text;
        }



        //采集设定点2间接停机点数据
        private void SendSecondIndirectStopMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x12, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x73, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveSecondIndirectStopMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveSecondIndirectStopMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x12)
                {
                     d = new SetTextCallback(SetSecondIndirectStopText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });

                }
            }
        }
      //  delegate void SetSecondIndirectStopTextCallback(string text);
        private void SetSecondIndirectStopText(string text)
        {
            SecondIndirectStop.Text = text;
        }


        //采集设定点2直接停机点数据
        private void SendSecondDirectStopMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x13, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x74, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveSecondDirectStopMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveSecondDirectStopMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x13)
                {
                     d = new SetTextCallback(SetSecondDirectStopText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
      //  delegate void SetSecondDirectStopTextCallback(string text);
        private void SetSecondDirectStopText(string text)
        {
            SecondDirectStop.Text = text;
        }


        //采集设定点选择数据
        private void SendSetpointSelectionMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x14, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x52, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveSetpointSelectionMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveSetpointSelectionMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x14)
                {
                     d = new SetTextCallback(SetSetpointSelectionText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
    //    delegate void SetSetpointSelectionTextCallback(string text);
        private void SetSetpointSelectionText(string text)
        {
            SetpointSelection.Text = text;
        }


        //采集压力带选择数据
        private void SendPressureBandSelectionMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x15, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x70, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceivePressureBandSelectionMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceivePressureBandSelectionMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x15)
                {
                     d = new SetTextCallback(SetPressureBandSelectionText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
    //    delegate void SetPressureBandSelectionTextCallback(string text);
        private void SetPressureBandSelectionText(string text)
        {
            PressureBandSelection.Text = text;
        }


        //采集最高转速因素
        private void SendYinsuMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x22, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x20, 0x00, 0x02 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveYinsuMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveYinsuMsg()
        {
            //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x15)
                {
                     d = new SetTextCallback(SetYinsuText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
        //    delegate void SetPressureBandSelectionTextCallback(string text);
        private void SetYinsuText(string text)
        {
            Yinsu.Text = text;
        }


        //采集运行时间数据
        private void SendOperationTimeMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x16, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xE8, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveOperationTimeMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveOperationTimeMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x16)
                {
                     d = new SetTextCallback(SetOperationTimeText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
     //   delegate void SetOperationTimeTextCallback(string text);
        private void SetOperationTimeText(string text)
        {
            OperationTime.Text = text;
        }


        //采集电机启动次数数据
        private void SendMotorStartTimesMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x17, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xea, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveMotorStartTimesMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveMotorStartTimesMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x17)
                {
                     d = new SetTextCallback(SetMotorStartTimesText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
    //    delegate void SetMotorStartTimesTextCallback(string text);
        private void SetMotorStartTimesText(string text)
        {
            MotorStartTimes.Text = text;
        }

        //采集加载继电器数据
        private void SendLoadRelayMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x18, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xeb, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveLoadRelayMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveLoadRelayMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x18)
                {
                     d = new SetTextCallback(SetLoadRelayText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
    //    delegate void SetLoadRelayTextCallback(string text);
        private void SetLoadRelayText(string text)
        {
            LoadRelay.Text = text;
        }

        //采集变频驱动1-20%速度数据
        private void SendFirstDriveSpeedMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x19, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xec, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveFirstDriveSpeedMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveFirstDriveSpeedMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x19)
                {
                     d = new SetTextCallback(SetFirstDriveSpeedText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });

                }
            }
        }
      //  delegate void SetFirstDriveSpeedTextCallback(string text);
        private void SetFirstDriveSpeedText(string text)
        {
            FirstDriveSpeed.Text = text;
        }

        //采集变频驱动20%-40%速度数据
        private void SendSecondDriveSpeedMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x1a, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xed, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveSecondDriveSpeedMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveSecondDriveSpeedMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x1a)
                {
                     d = new SetTextCallback(SetSecondDriveSpeedText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
     //   delegate void SetSecondDriveSpeedTextCallback(string text);
        private void SetSecondDriveSpeedText(string text)
        {
            SecondDriveSpeed.Text = text;
        }

        //采集变频驱动40%-60%速度数据
        private void SendThirdDriveSpeedMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x1b, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xee, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveThirdDriveSpeedMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();

        }

        //接收参数
        private void ReceiveThirdDriveSpeedMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x1b)
                {
                     d = new SetTextCallback(SetThirdDriveSpeedText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
      //  delegate void SetThirdDriveSpeedTextCallback(string text);
        private void SetThirdDriveSpeedText(string text)
        {
            ThirdDriveSpeed.Text = text;
        }


        //采集变频驱动60%-80%速度数据
        private void SendFourthDriveSpeedMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x1c, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xef, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveFourthDriveSpeedMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();

        }

        //接收参数
        private void ReceiveFourthDriveSpeedMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x1c)
                {
                     d = new SetTextCallback(SetFourthDriveSpeedText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
     //   delegate void SetFourthDriveSpeedTextCallback(string text);
        private void SetFourthDriveSpeedText(string text)
        {
            FourthDriveSpeed.Text = text;
        }


        //采集变频驱动80%-100%速度数据
        private void SendFifthDriveSpeedMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x1d, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xf0, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveFifthDriveSpeedMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();

        }

        //接收参数
        private void ReceiveFifthDriveSpeedMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x1d)
                {
                     d = new SetTextCallback(SetFifthDriveSpeedText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
    //    delegate void SetFifthDriveSpeedTextCallback(string text);
        private void SetFifthDriveSpeedText(string text)
        {
            FifthDriveSpeed.Text = text;
        }


        //采集风扇启动次数数据
        private void SendFanStartMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x1f, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xf2, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveFanStartMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveFanStartMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x1f)
                {
                     d = new SetTextCallback(SetFanStartText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
       // delegate void SetFanStartTextCallback(string text);
        private void SetFanStartText(string text)
        {
            FanStart.Text = text;
        }


        //采集累计流量数据
        private void SendCumulativeFlowMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x20, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xf3, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveCumulativeFlowMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveCumulativeFlowMsg()
        {
          //  while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x20)
                {
                     d = new SetTextCallback(SetCumulativeFlowText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });

                }
            }
        }
     //   delegate void SetCumulativeFlowTextCallback(string text);
        private void SetCumulativeFlowText(string text)
        {
            CumulativeFlow.Text = text;
        }

        //采集控制器运转时间数据
        private void SendControllerOperatingTimeMsg(object sender, EventArgs e)
        {
            byte[] data = new byte[] { 0x00, 0x21, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xf4, 0x00, 0x01 };
            newclient.Send(data);
            myThreadStart = new ThreadStart(ReceiveControllerOperatingTimeMsg);
            mThread = new Thread(myThreadStart);
            mThread.Start(); mThread.DisableComObjectEagerCleanup();
        }

        //接收参数
        private void ReceiveControllerOperatingTimeMsg()
        {
          //  while (true)
            {
               data = new byte[1024];//定义数据接收数组  
                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 
                if (data[1] == 0x21)
                {
                     d = new SetTextCallback(SetControllerOperatingTimeText);   // 托管调用  
                    this.Dispatcher.Invoke(d, new object[] { stringdata });
                }

            }
        }
     //   delegate void SetControllerOperatingTimeTextCallback(string text);
        private void SetControllerOperatingTimeText(string text)
        {
            ControllerOperatingTime.Text = text;
        }





        private void connect_Click(object sender, RoutedEventArgs e)
        {
            //校验端口号以及服务地址
            if (initIP())
            {
                Connect();

            }
        }
    }
}

